package changedb;


import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListModel;

/**
 *
 *
 * @author Ben Goodwin
 * @version @version 2.0 16/09/2011
 */
public class MembersList extends JList {

    private ArrayList<Member> members;

    public MembersList() {
        members = new ArrayList<Member>();
        setModel(arrayToModel(members));
    }

    public DefaultListModel arrayToModel(ArrayList<Member> m) {
        Member[] ma = new Member[m.size()];
        m.toArray(ma);
        Arrays.sort(ma);
        if (ma != null || ma.length > 0) {
            DefaultListModel model = new DefaultListModel();
            for (Member memb : ma) {
                model.addElement(memb);
            }

            return model;
        } else {
            return new DefaultListModel();
        }
    }

    public void setList(ArrayList<Member> m) {
        members = m;
        setModel(arrayToModel(members));
    }

    public ArrayList<Member> getMembers() {
        return members;
    }

    public ArrayList<Member> getMembersClone() {
        ArrayList<Member> clone = (ArrayList<Member>) members.clone();
        return clone;
    }

    public ArrayList<Member> getMembersClone(String filter) {
        ArrayList<Member> clone = (ArrayList<Member>) members.clone();
        if (filter.equals("All")) {
            return clone;
        } else {
            ArrayList<Member> filterClone = new ArrayList<Member>();
            for (Member m : clone) {
                if (m.getType().equals(filter)) {
                    filterClone.add(m);
                }
            }
            return filterClone;
        }
    }

    public boolean add(Member m) {
        try {
            members.add(m);
            setModel(arrayToModel(members));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean remove(Member m) {
        try {
            members.remove(m);
            setModel(arrayToModel(members));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public void filter(String filter) {
        ArrayList<Member> members2 = new ArrayList<Member>();
        if (filter.equals("All")) {
            members2 = members;
        } else {
            for (Member m : members) {
                if (m.getType().equals(filter)) {
                    members2.add(m);
                }
            }
        }
        setModel(arrayToModel(members2));
    }

    public void search(String text, String filter) {
        ArrayList<Member> filterMembers = new ArrayList<Member>();
        if (members != null && members.size() > 0) {
            for (Member m : members) {
                if (m != null) {
                    if ((filter.equals("All") || m.getDetails().get(0).equals(filter))
                            && m.getName().toLowerCase().contains(text.toLowerCase())) {
                        filterMembers.add(m);
                    }
                }
            }
            setModel(arrayToModel(filterMembers));
        }
    }

    public ListModel asModel() {
        return arrayToModel(members);
    }
}
